package cn.jiguang.demo;

import android.Manifest;
import android.app.NotificationChannel;
import android.app.NotificationChannelGroup;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import cn.jiguang.demo.view.BaseFragmentPagerAdapter;
import cn.jiguang.demo.baselibrary.ScreenUtils;
import cn.jiguang.demo.view.CustomMainTabItem;
import cn.jpush.android.api.JPluginPlatformInterface;

public class MainActivity extends AppCompatActivity {

    private TabLayout tabLayout;
    JPluginPlatformInterface jPluginPlatformInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.d_activity_main);
        initView();
        ScreenUtils.setStatusBarTransparent(getWindow());
        requestPermissions();
        initChannel();
        jPluginPlatformInterface = new JPluginPlatformInterface(this);

        Log.d("jiguang", "用户点击打开了通知");
        String data = null;
        //获取华为平台附带的jpush信息
        if (getIntent().getData() != null) {
            data = getIntent().getData().toString();
            Log.d("jiguang", "用户点击获取通知 -- " + data);
        }

    }
        protected void onStart() {
        super.onStart();
//        jPluginPlatformInterface.onStart(this);
    }
    protected void onStop() {
        super.onStop();
//        jPluginPlatformInterface.onStop(this);
    }
    private void requestPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 100);
        }
    }
    /**
     * 通过自定义通道设置自定义铃声
     */
    private void initChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (nm != null) {
                NotificationChannelGroup notificationChannelGroup = new NotificationChannelGroup("MyGroupId", "自定义通知组");
                nm.createNotificationChannelGroup(notificationChannelGroup);

                NotificationChannel notificationChannel = new NotificationChannel("MyChannelId", "自定义通知", NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setGroup("MyGroupId");
                notificationChannel.enableLights(true);
                notificationChannel.enableVibration(true);
                notificationChannel.setSound(Uri.parse("android.resource://com.HuananThirdPush.cn/raw/xiaoma"), null); //设置自定义铃声

                nm.createNotificationChannel(notificationChannel);
            }
        }
    }


    private void initView() {
        tabLayout = findViewById(R.id.tabLayout);
        final ViewPager viewPager = findViewById(R.id.viewPager);
        createTab();
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new AppInfoFragment());
        viewPager.setAdapter(new BaseFragmentPagerAdapter(getSupportFragmentManager(),null,fragments));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 1) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        int systemUiVisibility = getWindow().getDecorView().getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                        getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
                    }
                } else {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        int systemUiVisibility = getWindow().getDecorView().getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
                        getWindow().getDecorView().setSystemUiVisibility(systemUiVisibility);
                    }
                }

                viewPager.setCurrentItem(position,false);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void createTab(){

        List<Integer> mTitles = new ArrayList<>();
        List<Integer> sDrawable = new ArrayList<>();

        mTitles.add(R.string.tab_main);
        mTitles.add(R.string.tab_info);

        sDrawable.add(R.drawable.d_tab_main_selector);
        sDrawable.add(R.drawable.d_tab_info_selector);

        CustomMainTabItem.create()
                .setContext(this)
                .setTabLayout(tabLayout)
                .setTitleList(mTitles)
                .setIconList(sDrawable)
                .build();
    }
}